/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "BehaviorFactory.h"

#include "Behavior.h"


namespace galaxy {

	namespace entity {

		BehaviorFactory * BehaviorFactory::factory = new BehaviorFactory();

		BehaviorFactory::BehaviorFactory(void)
		{
		}

		BehaviorFactory::~BehaviorFactory(void)
		{
		}

		Behavior * BehaviorFactory::createBehavior (std::string& name) {
			BehaviorFactory * pf = BehaviorFactory::singleton ();
			BehaviorMap::iterator i = pf->behaviors.find(name);
			if (i != pf->behaviors.end()) {
				Behavior * objFactory = i->second;
				if (objFactory) {
					return objFactory->cloneBehavior();
				}
			}
			return (Behavior *) 0;
		}

		bool BehaviorFactory::registerBehavior (Behavior& behavior, std::string& name) {
			BehaviorFactory * pf = BehaviorFactory::singleton ();
			if (pf->behaviors.find (name) == pf->behaviors.end()) {
				// Behavior not already registered
				Behavior * factoryObj = behavior.cloneBehavior();
				if (factoryObj) {
					factoryObj->setState(Behavior::FACTORY);
					pf->behaviors[name] = factoryObj;
				}
			}
			return true;
		}

		BehaviorFactory * BehaviorFactory::singleton() {
			return factory;
		}

	} // namespace entity
} // namespace galaxy

